package com.atguigu.crowd.mvc.handler;

import com.atguigu.crowd.entity.Auth;
import com.atguigu.crowd.service.api.AuthService;
import com.atguigu.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/admin/authority/auth")
public class AuthHandler {

    @Autowired
    private AuthService authService;

    @ResponseBody
    @RequestMapping("/tree")
    public ResultEntity<List<Auth>> getAuthWholeTree() {
        List<Auth> authList = authService.getAllAuth();
        List<Auth> authTree = new ArrayList<>();
        for (Auth auth : authList) {
            if (auth.getCategoryId() == null) {
                Integer id = auth.getId();
                auth.setChildren(authService.getChildren(id));
                authTree.add(auth);
            }
        }

        return ResultEntity.successWithData(authTree);
    }
}
